package leafedbox.utils
{
	import mx.core.UIComponent;
	import mx.effects.Effect;
	import mx.effects.Parallel;
	import mx.effects.Pause;
	import mx.effects.Sequence;
	import mx.effects.SetPropertyAction;

	/**
	 * A utility class for working with effects.
	 */
	public class EffectUtil
	{
		//----------------------------------------------------------------------
		// Static methods
		//----------------------------------------------------------------------

		/**
		 * Creates a <code>Sequence</code> composite effect.
		 *
		 * @param targets  The UIComponent to serve as the target.
		 * @param duration 
		 *
		 * @return  An <code>Effect</code> object.
		 */
		public static function sequence(target:Object = null, ...children):Sequence
		{
			var effect : Sequence = new Sequence();
			setTarget(effect, target);
			effect.children = children;
			return effect;
		}


		/**
		 * Creates a <code>Parallel</code> composite effect.
		 *
		 * @param targets  The UIComponent to serve as the target.
		 * @param duration 
		 *
		 * @return  An <code>Effect</code> object.
		 */
		public static function parallel(target:Object = null, ...children):Parallel
		{
			var effect : Parallel = new Parallel();
			setTarget(effect, target);
			effect.children = children;
			return effect;
		}


		/**
		 * Creates an <code>Effect</code> from a hash property lookup.
		 *
		 * @param classRef  The class to instantiate.
		 * @param target    The target(s) of the effect.
		 * @param params    An <code>Object</code> lookup of the properties.
		 *
		 * @return  An <code>Effect</code> object.
		 */
		public static function create(classRef:Class, target:Object, params:Object=null):*
		{
			var effect : Effect = new classRef();
			setTarget(effect, target);

			if(params) {
				for(var paramName:String in params) {
					effect[paramName] = params[paramName];
				}
			}

			return effect;
		}


		/**
		 * Creates a <code>Pause</code> effect.
		 *
		 * @param duration  The duration.
		 *
		 * @return  An <code>Effect</code> object.
		 */
		public static function pause(duration:uint=500):Pause
		{
			var effect : Pause = new Pause();
			effect.duration = duration;
			return effect;
		}


		/**
		 * Creates a <code>SetPropertyAction</code> effect.
		 *
		 * @param target  The UIComponent to serve as the target.
		 * @param name    The property name.
		 * @param value   The value.
		 *
		 * @return  An <code>Effect</code> object.
		 */
		public static function setProperty(target:UIComponent, name:String, value:*):SetPropertyAction
		{
			var effect : SetPropertyAction = new SetPropertyAction();
			setTarget(effect, target);
			effect.name  = name;
			effect.value = value;
			return effect;
		}


		/**
		 * Sets the target for an effect.
		 *
		 * @param effect  The effect.
		 * @param target  The target (UIComponent) or targets (Array).
		 */
		public static function setTarget(effect:Effect, target:Object = null):void
		{
			if(target is Array) {
				effect.targets = target as Array;
			} else {
				effect.target = target as Object;
			}
		}
	}

}